package com.aidong.web.controller.card;

/**
 * Created by john on 16-11-3.
 */
import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.CardTypeCodeDao;
import com.aidong.dao.wills.MemberCardDao;
import com.aidong.model.wills.CardTypeCode;
import com.aidong.model.wills.MemberCard;
import com.aidong.web.controller.BaseController;
import java.util.ArrayList;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"cardDispatch"})
public class MemberCardDispatchController extends BaseController {
    @Resource
    MemberCardDao memberCardDao;
    @Resource
    CardTypeCodeDao cardTypeCodeDao;

    public MemberCardDispatchController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return (new ModelAndView("card/dispatch/list")).addObject("shopName", this.getTypeList("shopName")).addObject("cardStatus", this.getTypeList("cardStatus"));
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "status",required = false) Integer status, @RequestParam(value = "noFrom",required = false) String noFrom, @RequestParam(value = "noTo",required = false) String noTo, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 50);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        this.addConditionShopCode(cd, "shopCode", shopCode);
        cd.eq("status", status);
        cd.ge("cardCode", noFrom);
        cd.le("cardCode", noTo);
        this.memberCardDao.queryPageList(pg, cd, (new OrderSet()).asc("cardCode"));
        ModelAndView mv = new ModelAndView("card/dispatch/_list");
        mv.addObject("list", pg.getResultList());
        mv.addObject("pg", pg);
        return mv;
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        ModelAndView mv = new ModelAndView("card/dispatch/add");
        mv.addObject("shopName", this.getTypeList("shopName"));
        mv.addObject("cardTypeList", this.cardTypeCodeDao.getMakeCardList());
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@RequestParam("shopCode") String shopCode, @RequestParam("cardType") String cardType, @RequestParam("no1") String no1, @RequestParam("no2") String no2) {
        if(this.chkNull(shopCode)) {
            return this.getResult(false, "请选择收卡门店");
        } else {
            CardTypeCode cardTypeCode = (CardTypeCode)this.cardTypeCodeDao.getById(cardType);
            if(cardTypeCode == null) {
                return this.getResult(false, "卡种错误");
            } else if(this.chkNull(cardTypeCode.getMarkId())) {
                return this.getResult(false, "选择的卡种没有定义标识");
            } else if(!this.chkNull(no1) && !this.chkNull(no2)) {
                if(no1.length() != no2.length()) {
                    return this.getResult(false, "开始和结束卡号长度必须一致");
                } else if(this.memberCardDao.chkCardExists(cardTypeCode.getMarkId() + no1, cardTypeCode.getMarkId() + no2)) {
                    return this.getResult(false, "卡号已经存在");
                } else {
                    int nn = no1.length();

                    int n;
                    for(n = 0; n < nn - 1 && no1.charAt(n) == no2.charAt(n); ++n) {
                        ;
                    }

                    Integer n1 = this.parseInt(no1.substring(n));
                    Integer n2 = this.parseInt(no2.substring(n));
                    if(n1 != null && n2 != null) {
                        if(n2.intValue() - n1.intValue() > 10000) {
                            return this.getResult(false, "一次生成的卡太多");
                        } else {
                            String st = cardTypeCode.getMarkId() + no1.substring(0, n);
                            n = nn - n;
                            ArrayList cardList = new ArrayList();

                            for(int i = n1.intValue(); i <= n2.intValue(); ++i) {
                                String t = "00000000" + i;
                                cardList.add(st + t.substring(t.length() - n));
                            }

                            try {
                                this.memberCardDao.addCardList(cardList, cardTypeCode.getCode(), shopCode, this.getLoginUserCode());
                                this.addUserLog("配卡", "门店:" + this.cache.getValue("shopName", shopCode) + ",卡号:" + no1 + "-" + no2);
                            } catch (Exception var15) {
                                this.log.error("add card", var15);
                                return this.getResult(false, "保存发生错误");
                            }

                            return this.getSuccess(String.valueOf(cardList.size()), "生成了" + cardList.size() + "张卡");
                        }
                    } else {
                        return this.getResult(false, "序号部分必须是数字");
                    }
                }
            } else {
                return this.getResult(false, "请输入开始卡号和结束卡号");
            }
        }
    }

    @RequestMapping({"_del"})
    @ResponseBody
    public String _del(@RequestParam("id") String ids) {
        String[] ss = ids.split(",");
        ArrayList list = new ArrayList();
        String[] e = ss;
        int len$ = ss.length;

        for(int i$ = 0; i$ < len$; ++i$) {
            String c = e[i$];
            MemberCard card = this.memberCardDao.getByCardCode(c);
            if(card == null) {
                return this.getResult(false, "没有这个卡号:" + c);
            }

            if(card.getStatus().intValue() != 1) {
                return this.getResult(false, "已使用,不能删除:" + c);
            }

            list.add(card.getCardCode());
        }

        if(list.size() == 0) {
            return this.getResult(false, "请选择卡号");
        } else {
            try {
                this.memberCardDao.delCardList(list);
                this.addUserLog("删除配卡", list.size() + "张卡");
            } catch (Exception var9) {
                this.log.error("del card", var9);
                return this.getResult(false, "删除发生错误");
            }

            return this.getResult(true, "删除成功");
        }
    }
}